﻿using System;
using System.Collections;
using System.Windows.Forms;

namespace SedionFormDesigner.Infrastructure
{
    /// <summary>
    ///  用来显示host支持的服务
    ///  并分类(成功/失败)
    /// </summary>
    public class ServiceRequests : System.Windows.Forms.Form
    {
        private System.Windows.Forms.TreeView treeView1;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Label label1;
        private TreeNode successfulNode;
        private TreeNode unsuccessfulNode;
        private Hashtable serviceRequests;

        private System.ComponentModel.Container components = null;

        public ServiceRequests()
        {
            InitializeComponent();
            InitializeTreeViewNodes();
        }

        /// Create a new tree node to describe a service request of the given type.
        private TreeNode CreateNodeForType(Type type)
        {
            TreeNode node = new TreeNode(type.FullName);
            string stack = Environment.StackTrace;
            string[] entries = stack.Split('\r', '\n');

            bool recordEntry = false;
            foreach (string entry in entries)
            {
                if (entry.Length > 0)
                {
                    // Skip the frames that we're currently on.
                    //
                    if (recordEntry)
                    {
                        node.Nodes.Add(new TreeNode(entry));
                    }
                    else if (entry.IndexOf("GetService") != -1)
                    {
                        recordEntry = true;
                    }
                }
            }
            return node;
        }

        /// Set up our two root nodes.
        private void InitializeTreeViewNodes()
        {
            successfulNode = new TreeNode("Successful Requests");
            unsuccessfulNode = new TreeNode("Unsuccessful Requests");

            treeView1.Nodes.Add(successfulNode);
            treeView1.Nodes.Add(unsuccessfulNode);
        }

        public void ServiceFailed(Type type)
        {
            if (serviceRequests == null)
            {
                serviceRequests = new Hashtable();
            }

            if (!serviceRequests.ContainsKey(type))
            {
                serviceRequests[type] = false; // Mark the request for this type as failed
                unsuccessfulNode.Nodes.Add(CreateNodeForType(type)); // Add it to our tree
            }
        }

        public void ServiceSucceeded(Type type)
        {
            if (serviceRequests == null)
            {
                serviceRequests = new Hashtable();
            }

            if (!serviceRequests.ContainsKey(type))
            {
                serviceRequests[type] = true; // Mark the request for this type as successful
                successfulNode.Nodes.Add(CreateNodeForType(type)); // Add it to our tree
            }
        }

        /// Clean up any resources being used.
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.

        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(ServiceRequests));
            this.treeView1 = new System.Windows.Forms.TreeView();
            this.button1 = new System.Windows.Forms.Button();
            this.label1 = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // treeView1
            // 
            this.treeView1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
            | System.Windows.Forms.AnchorStyles.Left)
            | System.Windows.Forms.AnchorStyles.Right)));
            this.treeView1.Location = new System.Drawing.Point(0, 0);
            this.treeView1.Name = "treeView1";
            this.treeView1.Size = new System.Drawing.Size(440, 321);
            this.treeView1.TabIndex = 0;
            // 
            // button1
            // 
            this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this.button1.Location = new System.Drawing.Point(344, 382);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(86, 25);
            this.button1.TabIndex = 1;
            this.button1.Text = "&Clear";
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // label1
            // 
            this.label1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
            | System.Windows.Forms.AnchorStyles.Right)));
            this.label1.Location = new System.Drawing.Point(10, 338);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(324, 78);
            this.label1.TabIndex = 2;
            this.label1.Text = resources.GetString("label1.Text");
            // 
            // ServiceRequests
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
            this.ClientSize = new System.Drawing.Size(440, 422);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.treeView1);
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "ServiceRequests";
            this.Text = "ServiceRequests";
            this.ResumeLayout(false);

        }
        #endregion

        /// Clear the trees.
        private void button1_Click(object sender, System.EventArgs e)
        {
            treeView1.Nodes.Clear();
            InitializeTreeViewNodes();
            if (serviceRequests != null)
            {
                serviceRequests.Clear();
            }
        }
    }
}
